package com.quick.develop.framework.utils;


import lombok.extern.slf4j.Slf4j;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;


/**
 * @author hyd on 2022/11/4.
 * @version 1.0
 */
@Slf4j
public class XmlInUtil {

    /**
     * 读取指定路径的xml文件转换为Java对象
     *
     * @param xmlPath
     * @param load
     * @return
     */
    public static Object xmlToBean(String xmlPath, Class<?> load) {
        JAXBContext context = null;
        Object object = null;
        try {
            context = JAXBContext.newInstance(load);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            object = unmarshaller.unmarshal(new File(xmlPath));
        } catch (JAXBException e) {
            e.printStackTrace();
            log.error("xml解析失败:",e);
        }
        return object;
    }
}

